﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("264004")]
    public partial class SystemSpace : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            var dt_Directorys = new DataTable();
            DataRow dr_Directorys;

            dt_Directorys.Columns.Add(new DataColumn("Folder", Type.GetType("System.String")));
            dt_Directorys.Columns.Add(new DataColumn("Size", Type.GetType("System.String")));
            dt_Directorys.Columns.Add(new DataColumn("FileSize", Type.GetType("System.Int64")));

            var dirInfo = new DirectoryInfo(Server.MapPath("~/"));

            dr_Directorys = dt_Directorys.NewRow();
            dr_Directorys["Folder"] = @"\";

            var FileSize = DirSize(dirInfo.GetFiles());
            dr_Directorys["FileSize"] = FileSize;
            dr_Directorys["Size"] = dFileSize(FileSize);


            dt_Directorys.Rows.Add(dr_Directorys);


            foreach (var di in dirInfo.GetDirectories())
            {
                dr_Directorys = dt_Directorys.NewRow();
                dr_Directorys["Folder"] = di.Name;

                FileSize = DirSize(di);
                dr_Directorys["FileSize"] = FileSize;
                dr_Directorys["Size"] = dFileSize(FileSize);

                dt_Directorys.Rows.Add(dr_Directorys);
            }

            dr_Directorys = dt_Directorys.NewRow();
            dr_Directorys["Folder"] = @"总计";
            FileSize = Convert.ToInt64(dt_Directorys.Compute("sum(FileSize)", ""));
            dr_Directorys["Size"] = dFileSize(FileSize);
            dt_Directorys.Rows.Add(dr_Directorys);

            this.Repeater1.DataSource = dt_Directorys;
            this.Repeater1.DataBind();
        }

        private long DirSize(FileInfo[] fis)
        {
            long Size = 0;
            foreach (var fi in fis)
            {
                Size += fi.Length;
            }
            return Size;
        }

        private long DirSize(DirectoryInfo d)
        {
            long Size = 0;
            var fis = d.GetFiles();
            foreach (var fi in fis)
            {
                Size += fi.Length;
            }
            var dis = d.GetDirectories();
            foreach (var di in dis)
            {
                Size += DirSize(di);
            }
            return (Size);
        }

        private string dFileSize(double FileSize)
        {
            if (FileSize < 1024)
            {
                return FileSize + " B";
            }
            if ((FileSize < 1024*1024))
            {
                return Math.Round((FileSize/1024), 2) + " KB";
            }
            return Math.Round(FileSize/(1024*1024), 2) + " MB";
        }
    }
}